<?php
/**
 * Created by PhpStorm.
 * User: minicart
 * Date: 2018/9/21
 * Time: 16:17
 */
namespace app\models;
use think\Model;

/**
 * Class DictionaryModel
 * @package app\models
 */
class DictionaryModel extends Model
{
    protected $table = 'edu_dictionary';

    /**
     * @param $id
     * @param string $type
     * @return DictionaryModel|array|null
     * @throws \think\exception\DbException
     */
    public static function getById($id,$type = 'all')
    {
        if(!is_numeric($id)) {
            return [];
        }
        $dictionary = self::get(['id'=>$id]);
        if($dictionary) {
            return (isset($dictionary[$type])) ?  $dictionary[$type] : $dictionary;
        }
        return [];
    }

    /**
     * 订单列表 分页
     * @param array $where
     * @param int $page
     * @param int $limit
     * @param string $order
     * @return array
     * @throws \think\exception\DbException
     */
    public static function pageLists($where = [], $page = 1, $limit = 5, $order = 'id desc')
    {
        $result =self::where($where)->order($order)->paginate($limit,false,['page'=>$page]);
        if(!$result->total()){
            return ['code' => 0, 'msg' => "ok", 'count' =>0, 'data' => []];
        }
        $result = $result->toArray();
        return ['code' => 0, 'msg' => "ok", 'count' => $result['total'], 'data' => $result['data']];
    }

    /**
     * 类型描述
     * @param $value
     * @return string
     */
    public function getDescribeAttr($value)
    {
        return $value == "" ? '-' : $value;
    }


    /**
     * @param $param
     *
     * @throws \think\exception\DbException
     *
     * @return array
     */
    public function getPayTypeyLists($param)
    {
        $page = isset($param['page']) ? (int) $param['page'] : 1;
        $listRows = isset($param['limit']) ? (int) $param['limit'] : 10;
        $where = ['type' => 20];  //支付方式
        $where_ = [];
        if (isset($param['name']) && '' !== $param['name']) {
            $where['name'] = ['like', '%'.$param['name'].'%'];
        }

        //时间段
        if (isset($param['start_date']) && '' !== $param['start_date'] && isset($param['end_date']) && '' !== $param['end_date']) {
            $where_['paydate'] = array('between', array($param['start_date'],$param['end_date']));
        }

        $receivablesModel = new ReceivablesModel();
        $listData = self::where($where)
            ->paginate($listRows, false, ['page' => $page])
            ->toArray();
        $amount = 0;
        if (!empty($listData)) {

            foreach ($listData['data'] as &$item) {

                $item['pay_type'] =  $item['name'];
                $where_['payment_method'] =  $item['id'];
                $where_['status'] = 2; //已经审核通过
				if(get_current_type() == 4 || get_current_type() == 5){/*校区数据权限*/
					$where_['campus_id'] = get_current_campus_id();
				}elseif (get_current_type() == 2 || get_current_type() == 3){/*区域数据权限*/
					$where_['campus_id'] = ['in',get_current_campus_id()];
				}
                $money = ($receivablesModel->where($where_)->sum('amount') / 100);
                $amount = $amount + $money;
                $item['amount_money'] = $money;
            }
        }


        $item_end = [];
        $item_end['pay_type'] =  '合计';
        $item_end['amount_money'] =  $amount;
        $listData['data'][count($listData['data'])] = $item_end;


        return $listData;
    }


    /***招生来源分析***/

    public function getChannelStatisticsList($param){
        $page = isset($param['page']) ? (int) $param['page'] : 1;
        $listRows = isset($param['limit']) ? (int) $param['limit'] : 10;

        $where = ['type' => 2];
        $where_one = [];
        $where_two = [];
        $where_three = [];
        $where_four = [];
        $where_five = [];

        if(get_current_type() == 5){  //员工级别
            $where_one['user_id'] = get_current_id();
            $where_two['user_id'] = get_current_id();
            $where_three['user_id'] = get_current_id();
            $where_four['user_id'] = get_current_id();
            $where_five['user_id'] = get_current_id();
        }

        //时间段
        if (isset($param['start_date']) && '' !== $param['start_date'] && isset($param['end_date']) && '' !== $param['end_date']) {
      //    $where['update_time'] = array('between', array($param['start_date'].' 00:00:00',$param['end_date'].' 23:59:59'));
            $where_one['create_time'] = array('between', array($param['start_date'].' 00:00:00',$param['end_date'].' 23:59:59'));
            $where_two['create_time'] = array('between', array($param['start_date'].' 00:00:00',$param['end_date'].' 23:59:59'));
            $where_three['create_time'] = array('between', array($param['start_date'].' 00:00:00',$param['end_date'].' 23:59:59'));
            $where_four['create_time'] = array('between', array($param['start_date'].' 00:00:00',$param['end_date'].' 23:59:59'));
            $where_five['create_time'] = array('between', array($param['start_date'].' 00:00:00',$param['end_date'].' 23:59:59'));
        }

        $listData = self::where($where)
            ->paginate($listRows, false, ['page' => $page])
            ->toArray();

        $successful_transformation = 0;
        $StudentModel = new StudentModel();

        $where_five['status'] = 1;
        $total_count = $StudentModel->where($where_five)->count();

        if (!empty($listData)) {
            foreach ($listData['data'] as &$item) {

                $where_one['status'] = 1;
                $where_one['channel'] = $item['id'];
                $item['count'] = $StudentModel->where($where_one)->count();

                if($total_count <= 0){
                    $item['rate'] = '0%';
                }else{
                    $item['rate'] =    sprintf("%.2f",(($item['count'] / $total_count) * 100)).'%';
                }

                $where_two['status'] = 1;
                $where_two['sign_up'] = 1;
                $where_two['channel'] = $item['id'];
                $item['successful_transformation'] = $StudentModel->where($where_two)->count();

                if($item['count'] <= 0){
                    $item['successful_transformation_rate'] = '0%';
                }else{
                    $item['successful_transformation_rate'] =  sprintf("%.2f",(($item['successful_transformation'] / $item['count']) * 100)).'%';
                }
                $successful_transformation = $successful_transformation +   $item['successful_transformation'];

            }

            $item_end = [];
            $item_end['name'] =  '转介绍';

            $where_three['status'] = 1;
            $where_three['channel'] = -1;
            $item_end['count'] =  $StudentModel->where($where_three)->count();

            if($total_count <= 0){
                $item_end['rate'] = '0%';
            }else{
                $item_end['rate'] =    sprintf("%.2f",(($item_end['count'] / $total_count) * 100)).'%';
            }

            $where_four['status'] = 1;
            $where_four['sign_up'] = 1;
            $where_four['channel'] = -1;
            $item_end['successful_transformation'] =  $StudentModel->where($where_four)->count();;

            if($item_end['count'] <= 0){
                $item_end['successful_transformation_rate'] = '0%';
            }else{
                $item_end['successful_transformation_rate'] =  sprintf("%.2f",(($item_end['successful_transformation'] / $item_end['count']) * 100)).'%';
            }
            $successful_transformation = $successful_transformation + $item_end['successful_transformation'];
            $listData['data'][count($listData['data'])] = $item_end;



            $item_end = [];
            $item_end['name'] =  '合计';
            $item_end['count'] =  $total_count;
            $item_end['rate'] =  '---';
            $item_end['successful_transformation'] =  $successful_transformation;

            if($item_end['count'] <= 0){
                $item_end['successful_transformation_rate'] = '0%';
            }else{
                $item_end['successful_transformation_rate'] =  sprintf("%.2f",(($item_end['successful_transformation'] / $item_end['count']) * 100)).'%';
            }

            $listData['data'][count($listData['data'])] = $item_end;

        }
        return $listData;
    }


    /***单个校区招生来源分析***/

    public function getChannelOnlyStatisticsList($param){
        $page = isset($param['page']) ? (int) $param['page'] : 1;
        $listRows = isset($param['limit']) ? (int) $param['limit'] : 10;
        $campus_id = $param['campus_id'];
        $where = ['type' => 2];

        $where_one = [];
        $where_two = [];
        $where_three = [];
        $where_four = [];
        $where_five = [];

        if(get_current_type() == 5){  //员工级别
            $where_one['user_id'] = get_current_id();
            $where_two['user_id'] = get_current_id();
            $where_three['user_id'] = get_current_id();
            $where_four['user_id'] = get_current_id();
            $where_five['user_id'] = get_current_id();
        }


        //时间段
        if (isset($param['start_date']) && '' !== $param['start_date'] && isset($param['end_date']) && '' !== $param['end_date']) {
            //    $where['update_time'] = array('between', array($param['start_date'].' 00:00:00',$param['end_date'].' 23:59:59'));
            $where_one['create_time'] = array('between', array($param['start_date'].' 00:00:00',$param['end_date'].' 23:59:59'));
            $where_two['create_time'] = array('between', array($param['start_date'].' 00:00:00',$param['end_date'].' 23:59:59'));
            $where_three['create_time'] = array('between', array($param['start_date'].' 00:00:00',$param['end_date'].' 23:59:59'));
            $where_four['create_time'] = array('between', array($param['start_date'].' 00:00:00',$param['end_date'].' 23:59:59'));
            $where_five['create_time'] = array('between', array($param['start_date'].' 00:00:00',$param['end_date'].' 23:59:59'));
        }


        $listData = self::where($where)
            ->paginate($listRows, false, ['page' => $page])
            ->toArray();

        $successful_transformation = 0;
        $StudentModel = new StudentModel();

        $where_five['status'] = 1;
        $where_five['campus_id'] = $campus_id;
        $total_count = $StudentModel->where($where_five)->count();

        if (!empty($listData)) {
            foreach ($listData['data'] as &$item) {
                $where_one['status'] = 1;
                $where_one['campus_id'] = $campus_id;
                $where_one['channel'] = $item['id'];

                $item['count'] = $StudentModel->where($where_one)->count();

                if($total_count <= 0){
                    $item['rate'] = '0%';
                }else{
                    $item['rate'] =    sprintf("%.2f",(($item['count'] / $total_count) * 100)).'%';
                }

                $where_two['status'] = 1;
                $where_two['campus_id'] = $campus_id;
                $where_two['sign_up'] = 1;
                $where_two['channel'] =  $item['id'];
                $item['successful_transformation'] = $StudentModel->where($where_two)->count();

                if($item['count'] <= 0){
                    $item['successful_transformation_rate'] = '0%';
                }else{
                    $item['successful_transformation_rate'] =  sprintf("%.2f",(($item['successful_transformation'] / $item['count']) * 100)).'%';
                }
                $successful_transformation = $successful_transformation +   $item['successful_transformation'];

            }

            $item_end = [];
            $item_end['name'] =  '转介绍';

            $where_three['status'] = 1;
            $where_three['campus_id'] = $campus_id;
            $where_three['channel'] = -1;
            $item_end['count'] =  $StudentModel->where($where_three)->count();;

            if($total_count <= 0){
                $item_end['rate'] = '0%';
            }else{
                $item_end['rate'] =    sprintf("%.2f",(($item_end['count'] / $total_count) * 100)).'%';
            }

            $where_four['status'] = 1;
            $where_four['campus_id'] = $campus_id;
            $where_four['sign_up'] = 1;
            $where_four['channel'] = -1;
            $item_end['successful_transformation'] =  $StudentModel->where($where_four)->count();;

            if($item_end['count'] <= 0){
                $item_end['successful_transformation_rate'] = '0%';
            }else{
                $item_end['successful_transformation_rate'] =  sprintf("%.2f",(($item_end['successful_transformation'] / $item_end['count']) * 100)).'%';
            }
            $successful_transformation = $successful_transformation + $item_end['successful_transformation'];
            $listData['data'][count($listData['data'])] = $item_end;



            $item_end = [];
            $item_end['name'] =  '合计';
            $item_end['count'] =  $total_count;
            $item_end['rate'] =  '---';
            $item_end['successful_transformation'] =  $successful_transformation;

            if($item_end['count'] <= 0){
                $item_end['successful_transformation_rate'] = '0%';
            }else{
                $item_end['successful_transformation_rate'] =  sprintf("%.2f",(($item_end['successful_transformation'] / $item_end['count']) * 100)).'%';
            }

            $listData['data'][count($listData['data'])] = $item_end;

        }
        return $listData;
    }
}